package demo.jmeter;

import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @date 2024/12/9 10:00
 */
public class HelloJavaSample extends AbstractJavaSamplerClient {
    private AtomicInteger counter = new AtomicInteger(0);
    @Override
    public void setupTest(JavaSamplerContext context) {
        super.setupTest(context);
    }

    @Override
    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);
    }

    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        System.out.println("hello world!"+counter.incrementAndGet());
        SampleResult sampleResult = new SampleResult();
        sampleResult.sampleStart();
        try {
            Thread.sleep(1);
            sampleResult.setSuccessful(true);
        } catch (InterruptedException e) {
            e.printStackTrace();
            sampleResult.setSuccessful(false);
        }
        sampleResult.sampleEnd();
        sampleResult.setResponseCode("200");
        sampleResult.setResponseMessage("success!");
        sampleResult.setSamplerData("hello");
        sampleResult.setSentBytes("hello".getBytes().length);
        sampleResult.setResponseData("hello world!".getBytes());
        return sampleResult;
    }
}
